package com.atguigui.leetcode1;

import java.util.HashMap;
import java.util.Map;

/**
 * 1487.保证文件名唯一
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by WJX on 2023/3/3 8:46
 */
public class P1487MakingFileNamesUnique {

    public static void main(String[] args) {
        Solution solution = new P1487MakingFileNamesUnique().new Solution();
        // TO TEST
    }

    class Solution {
        public String[] getFolderNames(String[] names) {
            Map<String, Integer> index = new HashMap<String, Integer>();
            int n = names.length;
            String[] res = new String[n];
            for (int i = 0; i < n; i++) {
                String name = names[i];
                //不包含直接添加
                if (!index.containsKey(name)) {
                    res[i] = name;
                    index.put(name, 1);
                } else {
                    //第几个重复了
                    int k = index.get(name);
                    while (index.containsKey(addSuffix(name, k))) {
                        k++;
                    }
                    res[i] = addSuffix(name, k);
                    index.put(name, k + 1);
                    //将增加 ( ) 放入map
                    index.put(addSuffix(name, k), 1);
                }
            }

            return res;
        }

        public String addSuffix(String name, int k) {
            return name + "(" + k + ")";
        }
    }
}
